@include('admin.header')
<style>
  .search_product {
    max-height:248px;
    overflow-y: scroll;
  }
  .search_product > .item {
    width:80px;
    margin-right:5px;
    float:left;
  }
  .search_product > .item img {
    height:80px;
  }
  .search_product > .item .name {
    height:20px;
    overflow:hidden;
  }
</style>
<div class="margin-top-20">
  <form method="post" class="form-horizontal js-ajax-form direct_add_form" action="/globalexchange/direct_group_add_submit" novalidate="novalidate" enctype ="multipart/form-data">
    <fieldset>
      <div class="control-group"><label class="control-label">套餐名称</label>
        <div class="controls">
          <input type="text" name="name" value="{{$data->name or ''}}"><span class="form-required" >*</span>
        </div>
      </div>
      <div class="control-group"><label class="control-label">套餐图片</label>
        <div class="controls">
          @if(!empty($data->image))
            <img src="{{$data->image}}" style="height:80px;" />
          @endif
          <input type="file" name="image" onchange="upload_image(this)" />
        </div>
      </div>
      <div class="control-group"><label class="control-label">商品ID</label>
        <div class="controls"><input type="text" name="product_id" readonly value="{{$data->product_id or ''}}"><span class="form-required">*逗号分隔</span></div>
        <div class="controls"><input type="text" class="product_search_keyword" placeholder="搜索商品名并添加"><a class="btn product_search"><i class="fa fa-search"></i> 搜索</a></div>
        <div class="controls search_product" id="app">
          
        </div>
      </div>
      <div class="control-group"><label class="control-label">套餐利润</label>
        <div class="controls">
          <input type="text" name="profit" value="{{$data->profit or ''}}"><span class="form-required" >*</span>
        </div>
      </div>
      <div class="control-group"><label class="control-label">PV</label>
        <div class="controls">
          <input type="text" name="pv" value="{{$data->pv or ''}}"><span class="form-required" >*必须为100的倍数</span>
        </div>
      </div>
    </fieldset>
    <div class="form-actions"><input type="hidden" name="id" value="1">
      <input type="hidden" name="id" value="{{$data->id or 0}}" />
      <button type="submit" class="btn btn-primary js-ajax-submit">保存</button></div>
  </form>
</div>

<script>
	$(function () {
		$('.direct_add_form').submit(function(){
			var $t = $(this),action=$t.attr('action'),data = $t.serialize();
			post(action,data,function(data){
				_alert('处理成功',function(){
					parent.location.reload();
					parent.layer.close(parent.layer_open2);
                });
            });
			return false;
        });
		
		$('.product_search').click(function(){
			var keyword = $('.product_search_keyword').val();
			var $t = $(this),url = "/globalexchange/direct_product_search",data = {keyword:keyword};
			
			post(url,data,function(data){
				var dom = '',d = [];
				for(i in data){
					d = data[i];
					dom += '<div class="item"><img class="width100" title="'+d.product_name+'" src="'+d.image+'" /><div class="name">'+d.product_name+'</div><a class="btn btn-primary btn-small btn-block" onclick="product_add('+d.product_id+')">添加</a></div>'
				}
				$('.search_product').html(dom);
            });
        });
	});
	
	function product_add(product_id){
		var $t = $('[name=product_id]'),val = $t.val(),vals = val.split(',');
		product_id = product_id + '';
		if(vals.length == 1){
			if(vals[0] == '')
			  vals = [];
        }
		if($.inArray(product_id,vals) === -1){
			vals.push(product_id);
			val = vals.join(',');
        }
		$t.val(val);
    }
</script>